import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { I18nValidationPipe } from 'nestjs-i18n';
import { AppModule } from './app.module';
import { AllExceptionFilter } from './common/filters/all-exception.filter';
import { I18nValidationExceptionFilter } from './common/filters/i18n-validation-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    rawBody: true,
  });
  app.useBodyParser('text');

  app.useGlobalPipes(
    new I18nValidationPipe(),
    // 自动删除非白名单属性（那些在验证类dto中没有任何装饰器的属性）
    new ValidationPipe({ whitelist: true }),
  );
  const configService = app.get(ConfigService);
  app.setGlobalPrefix(configService.get<string>('API_PREFIX', 'api'));
  app.useGlobalFilters(
    new AllExceptionFilter(),
    new I18nValidationExceptionFilter({
      detailedErrors: false,
    }),
  );
  await app.listen(configService.get<number>('PORT', 3000));
}
bootstrap();
